/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vasthu.core.dao;

import com.vasthu.api.dao.FunctionDAO;
import com.vasthu.api.domain.Function;
import com.vasthu.utility.VstDBManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author moremada
 */
public class FunctionDAOImpl implements FunctionDAO {

    static Logger logger = Logger.getLogger(FunctionDAOImpl.class);

    @Override
    public boolean insertFunction(Function function) {
        if (function == null) {
            return false;
        }
        Connection con = VstDBManager.get().getConnection();
        PreparedStatement pst = null;

        try {
            pst = con.prepareStatement("INSERT INTO Function(function, description) VALUES(?, ?)");

            pst.setString(1, function.getFuncName());
            pst.setString(2, function.getDescription());

            pst.executeUpdate();

        } catch (Exception ex) {
            System.out.println("ERROR : FunctionDAOImpl->insertFunction()");
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }

    @Override
    public Function getFunctionById(int funId) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Function fun = new Function();
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("SELECT * FROM Function WHERE f_id = ?");
            pst.setLong(1, funId);
            rs = pst.executeQuery();

            while (rs.next()) {
                fun.setfId(rs.getInt("f_id"));
                fun.setFuncName(rs.getString("function"));
                fun.setDescription(rs.getString("description"));
                break;
            }

        } catch (Exception ex) {
            System.out.println("ERROR : FunctionDAOImpl->getFunctionById() " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return fun;
    }

    @Override
    public Function getFunctionByName(String funcName) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Function fun = new Function();
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("SELECT * FROM Function WHERE function = ?");
            pst.setString(1, funcName);
            rs = pst.executeQuery();

            while (rs.next()) {
                fun.setfId(rs.getInt("f_id"));
                fun.setFuncName(rs.getString("function"));
                fun.setDescription(rs.getString("description"));
                break;
            }

        } catch (Exception ex) {
            System.out.println("ERROR : FunctionDAOImpl->getFunctionByName() " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return fun;
    }

    @Override
    public boolean deleteFunctionById(int functionId) {
        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("DELETE FROM Function WHERE f_id = ?");
            pst.setLong(1, functionId);
            pst.executeUpdate();
        } catch (Exception ex) {
            System.out.println("ERROR : FunctionDAOImpl->deleteFunctionById() " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }

    @Override
    public List<Function> getAllFunctions() {
        List<Function> allFunctions = new ArrayList<Function>();
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("SELECT * FROM Function");
            rs = pst.executeQuery();

            while (rs.next()) {
                Function fun = new Function();

                fun.setfId(rs.getInt("f_id"));
                fun.setFuncName(rs.getString("function"));
                fun.setDescription(rs.getString("description"));

                allFunctions.add(fun);
            }

        } catch (SQLException ex) {
            System.out.println("ERROR : FunctionDAOImpl->getAllFunctions() " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return allFunctions;
    }
}
